﻿using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

namespace MTP
{


    public interface IDispatchClient : IDisposable
    {
        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="iPEndPoint"></param>
        Task<IDispatchClient> ConnectAsync(EndPoint endPoint);

        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="hostName"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        Task<IDispatchClient> ConnectAsync(string hostName, int port);
        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="address"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        Task<IDispatchClient> ConnectAsync(IPAddress address, int port);

        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="iPEndPoint"></param>
        Task<IDispatchClient> ConnectAsync(EndPoint endPoint, CancellationToken cancellationToken);

        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="hostName"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        Task<IDispatchClient> ConnectAsync(string hostName, int port, CancellationToken cancellationToken);
        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="address"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        Task<IDispatchClient> ConnectAsync(IPAddress address, int port, CancellationToken cancellationToken);
        /// <summary>
        /// 设置超时重连时间 秒
        /// </summary>
        /// <param name="outTime"></param>
        /// <returns></returns>
        IDispatchClient ReConnect(int outTime);
        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        Task<MTPPackage> SendAsync(MTPPackage data, int timeout = 30000);


        /// <summary>
        /// 连接成功
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        IDispatchClient OnConnected(Action<IDispatchClient> connected);

        /// <summary>
        /// 连接断开
        /// </summary>
        /// <param name="connected"></param>
        /// <returns></returns>
        IDispatchClient OnDisconnect(Action connected);


        /// <summary>
        /// 接收消息时
        /// </summary>
        /// <param name="clientReceive"></param>
        /// <returns></returns>
        IDispatchClient OnReceive(Action<MTPPackage> clientReceive);
    }
}
